%% This code plots the country graphs created by make_country_graphs.m

clear
close all
clc

folders;

set(0,'DefaultFigureWindowStyle','normal','DefaultFigureVisible','on') % I stopped doing the docked thing because it doesn't allow to specify size and gives inconsistent results across computers

addpath('../../Toolbox/export_fig/');
addpath('../../Toolbox/struct2csv/');

graph_width=800;
graph_height=600;

%% Displaying maps 
% which group of countries to display?
group = 2; %%  1=MERCOSUR, 2 = ANDES

% common parameters to all graphs
bright_line = 0.5;  % higher=brighter
bright_nodes = 0.4; % higher=brighter
max_bright = 5;    % higher=darker lines
adj_width = 0.3;
markersize = 5;
color_growth = [ 0 1 0 ];   % red = [ 1 0 0 ]  green = [ 0 1 0 ] blue = [ 0 0 1 ]
color_shrink = [ 1 0 0 ];
Nquantile=10;

% load in grids 
if group == 1    
    load( [ path_save_grids, 'MERCOSUR_grid.mat' ]);
elseif group == 2
     load( [ path_save_grids, 'ANDES_grid.mat' ]);
end

% load components
% Export data to graph for calibration
places_grid=country_graph.places_grid;
gridmap=country_graph.gridmap;
discretized_roads = country_graph.discretized_roads;
edges = country_graph.edges;  
unique_edges = country_graph.unique_edges;      

% number of categories for plotting
N_cat = numel( unique( cell2mat( {gridmap.quantiles} ) ) )-1;


% load in country border shapefiles
if group == 1    
    uy =  load([ datafolder_LAC, 'UY/','country_bounds.mat']);
    py =  load([ datafolder_LAC, 'PY/','country_bounds.mat']);
    ar =  load([ datafolder_LAC, 'AR/','country_bounds.mat']);
    br2 = shaperead([ '../../data/COUNTRY_SHP/countries_shp/countries']);
    br = br2(strcmp({br2(:).NAME}, 'Brazil'));              

elseif group == 2
    pe =  load(['../codes_old/country grids/Peru_grid_0_0_0.5_LAC.mat']);
    bo =  load([ datafolder_LAC, 'BO/','country_bounds.mat']);
    co =  load([ datafolder_LAC, 'CO/','country_bounds.mat']);
    ec =  load([ datafolder_LAC, 'EC/','country_bounds.mat']);
end

close(figure(1))
figure(1)
mapshow( gridmap, 'FaceColor' , 'black')

if group == 2
    mapshow( pe.country_graph.country_bounds,...
                'FaceColor','black' )
end

for i=N_cat+1:-1:1
    relsize = bright_nodes+(1-bright_nodes)*i/( N_cat+1 );
    mapshow( gridmap( cell2mat( {gridmap.quantiles} )==i ),...
        'FaceColor',relsize*[ 0 1 1 ] )
end

if group == 1

    mapshow( uy.country_bounds,...
        'FaceColor','none',...
        'EdgeColor','m','LineWidth',  2.5 )

    mapshow( py.country_bounds,...
        'FaceColor','none',...
        'EdgeColor','m','LineWidth',  2.5 )

    mapshow( ar.country_bounds,...
        'FaceColor','none',...
        'EdgeColor','m','LineWidth',  2.5 )

    mapshow( br,...
        'FaceColor','none',...
        'EdgeColor','m','LineWidth', 2.5 )

end

if group == 2
    mapshow( pe.country_graph.country_bounds,...
                    'FaceColor','none',...
                    'EdgeColor','m','LineWidth',  2.5 )

    mapshow( bo.country_bounds,...
                    'FaceColor','none',...
                    'EdgeColor','m','LineWidth',  2.5 )

    mapshow( co.country_bounds,...
                    'FaceColor','none',...
                    'EdgeColor','m','LineWidth',  2.5 )

    mapshow( ec.country_bounds,...
                    'FaceColor','none',...
                    'EdgeColor','m','LineWidth',  2.5 )               
end

set( gca,'XTick',[],'YTick',[], 'Box','on' )       
export_fig([ path_notes_images,'group', num2str(group),'_pop.eps' ], '-depsc');

% Income
close(figure(2))
figure(2)
for i=N_cat+1:-1:1
            relsize = bright_nodes+(1-bright_nodes)*i/( N_cat+1 );
            mapshow( gridmap( cell2mat( {gridmap.quantiles_inc} )==i ),...
                'FaceColor',relsize*[ 0 1 1 ] )
end


set( gca,'XTick',[],'YTick',[], 'Box','on' )       
export_fig([ path_notes_images,'group', num2str(group),'_income.eps' ], '-depsc');

% Baseline grid: Nodes and Edges
close(figure(3))
figure(3)
markersize = 3;

if group == 2
    mapshow( pe.country_graph.country_bounds,...
                'FaceColor','black' )
end

mapshow( gridmap,...
                   'FaceColor','Black',...
                   'EdgeColor','White','LineWidth', 0.5 )

mapshow( discretized_roads,...
                            'Color',[ 1 0 0 ],...
                            'LineWidth', 1  )

                                                    % centroids
mapshow( places_grid,...
                    'Marker','o',...
                    'MarkerFaceColor',[ 0 1 1 ],...
                    'MarkerEdgeColor',[ 0 0 0 ],...
                    'MarkerSize',markersize )

if group == 1

    mapshow( uy.country_bounds,...
        'FaceColor','none',...
        'EdgeColor','m','LineWidth',  2.5 )

    mapshow( py.country_bounds,...
        'FaceColor','none',...
        'EdgeColor','m','LineWidth',  2.5 )

    mapshow( ar.country_bounds,...
        'FaceColor','none',...
        'EdgeColor','m','LineWidth',  2.5 )

    mapshow( br,...
        'FaceColor','none',...
        'EdgeColor','m','LineWidth', 2.5 )

end

if group == 2

    mapshow( pe.country_graph.country_bounds,...
                    'FaceColor','none',...
                    'EdgeColor','m','LineWidth',  2.5 )

    mapshow( bo.country_bounds,...
                    'FaceColor','none',...
                    'EdgeColor','m','LineWidth',  2.5 )

    mapshow( co.country_bounds,...
                    'FaceColor','none',...
                    'EdgeColor','m','LineWidth',  2.5 )

    mapshow( ec.country_bounds,...
                    'FaceColor','none',...
                    'EdgeColor','m','LineWidth',  2.5 )  
end

set( gca,'XTick',[],'YTick',[], 'Box','on' )       
export_fig([ path_notes_images,'group', num2str(group),'_grid.eps' ], '-depsc');

% Discretized Roads 
temp =  cell2mat({ discretized_roads.avI })';
quantiles_avI_grid = quantile( temp( temp>0 ), Nquantile  );  % assign quantiles conditional on positive population in the square
for i=1:length( discretized_roads)
    if temp(i)>0
        [  discretized_roads(i).quantiles ] = sum(  discretized_roads(i).avI>quantiles_avI_grid )+1;
    elseif temp(i)==0
        [  discretized_roads(i).quantiles ] = 0;
    end
end

close(figure(4))
figure(4)
markersize = 3;

if group == 2
    mapshow( pe.country_graph.country_bounds,...
            'FaceColor','black' )
end

mapshow( gridmap,...
                   'FaceColor','Black',...
                   'EdgeColor','White','LineWidth', 0.5 )

if group == 1

    mapshow( uy.country_bounds,...
        'FaceColor','none',...
        'EdgeColor','m','LineWidth',  2.5 )

    mapshow( py.country_bounds,...
        'FaceColor','none',...
        'EdgeColor','m','LineWidth',  2.5 )

    mapshow( ar.country_bounds,...
        'FaceColor','none',...
        'EdgeColor','m','LineWidth',  2.5 )

    mapshow( br,...
        'FaceColor','none',...
        'EdgeColor','m','LineWidth', 2.5 )

end

if group == 2

    mapshow( pe.country_graph.country_bounds,...
                    'FaceColor','none',...
                    'EdgeColor','m','LineWidth',  2.5 )

    mapshow( bo.country_bounds,...
                    'FaceColor','none',...
                    'EdgeColor','m','LineWidth',  2.5 )

    mapshow( co.country_bounds,...
                    'FaceColor','none',...
                    'EdgeColor','m','LineWidth',  2.5 )

    mapshow( ec.country_bounds,...
                    'FaceColor','none',...
                    'EdgeColor','m','LineWidth',  2.5 )  
end

for i=N_cat+1:-1:1
    relsize = bright_nodes+(1-bright_nodes)*i/( N_cat+1 );
    mapshow( discretized_roads(  cell2mat( {discretized_roads.quantiles} )==i ),...
                        'Color',relsize*[0 1 0],...
                        'LineWidth',i/11*3 )
end 
            % centroids
mapshow( places_grid,...
                    'Marker','o',...
                    'MarkerFaceColor',[ 0 1 1 ],...
                    'MarkerEdgeColor',[ 0 0 0 ],...
                    'MarkerSize',markersize )

set( gca,'XTick',[],'YTick',[], 'Box','on' )       
export_fig([ path_notes_images,'group', num2str(group),'_roads.eps' ], '-depsc');



